package com.hgyc.mom.base.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.base.entity.Supplier;
import com.hgyc.mom.base.vo.SupplierParam;
import com.hgyc.mom.base.service.SupplierService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 供应商Controller
 *
 * @author fwj
 * @since 2025-05-15
 */
@Tag(name = "供应商控制器", description = "供应商控制器")
@RestController
@RequestMapping("/base/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @Operation(summary = "查询供应商列表", description = "查询供应商列表")
    @PostMapping("/pageList")
    public Result<IPage<Supplier>> pageList(@Parameter(description = "查询参数") @RequestBody SupplierParam supplierParam) {
        Page<Supplier> page = new Page<>(supplierParam.getPage(), supplierParam.getPageSize());
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierParam, supplier);
        QueryWrapper<Supplier> wrapper = QueryWrapperUtil.build(supplier);
        IPage<Supplier> list = supplierService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        util.importTemplateExcel(response, "供应商");
    }

    @Operation(summary = "导出供应商列表", description = "导出供应商列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplierParam supplierParam) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierParam, supplier);
        QueryWrapper<Supplier> wrapper = QueryWrapperUtil.build(supplier);
        List<Supplier> list = supplierService.list(wrapper);
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        util.exportExcel(response, list, "供应商数据");
    }

    @Operation(summary = "导入供应商列表", description = "导入供应商列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        List<Supplier> supplierList = util.importExcel(file.getInputStream());
        String message = supplierService.importData(supplierList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取供应商详细信息", description = "获取供应商详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "供应商ID") @PathVariable("id") Long id) {
        return Result.success(supplierService.getById(id));
    }

    @Operation(summary = "新增供应商", description = "新增供应商")
    @PostMapping
    public Result save(@Parameter(description = "供应商") @RequestBody Supplier supplier) {
        return Result.success(supplierService.save(supplier));
    }

    @Operation(summary = "修改供应商", description = "修改供应商")
    @PutMapping
    public Result update(@Parameter(description = "供应商") @RequestBody Supplier supplier) {
        return Result.success(supplierService.updateById(supplier));
    }

    @Operation(summary = "删除供应商", description = "删除供应商")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(supplierService.removeByIds(Arrays.asList(ids)));
    }
}